package cn.intertion.service.impl;

import cn.intertion.model.wxObj;
import cn.intertion.service.WxToken;
import cn.intertion.utils.JSONUtil;
import cn.intertion.utils.URLUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class WxTokenImpl implements WxToken {

    private static final Logger log = LoggerFactory.getLogger(WxTokenImpl.class);

    /**
     * 刷新token
     */
    @Override
    public void setWXAccessToken() throws Exception {
        log.error("开始获取accesstoken，原accessToken时间：" + new Date(wxObj.tokenDate).toLocaleString());
        String tmp = URLUtils.doGet(wxObj.http0);
        tmp = JSONUtil.getString(tmp, "access_token");

        if (tmp != null && !tmp.equals("")) {
            log.error("获取accesstoken成功：" + tmp + " getTime: " + new Date().toLocaleString());
            wxObj.accessToken = tmp;
            System.out.println(wxObj.accessToken);
            wxObj.tokenDate = System.currentTimeMillis();
            String http8 = "http://chart.intertion.net:9091/jeecg-boot/otherApi/setAccessToken?t=" + tmp;
            URLUtils.doGet(http8);
            String http9 = "http://ishipscome.intertion.cn/jeecg-boot/otherApi/setAccessToken?t=" + tmp;
            URLUtils.doGet(http9);
        } else {
            log.error("获取accesstoken失败：" + " getTime: " + new Date().toLocaleString());
        }
        String tmp1 = URLUtils.doGet(wxObj.http1.replace("@@@@@", wxObj.accessToken));
        tmp1 = JSONUtil.getString(tmp1, "ticket");
        if (tmp1 != null && !tmp1.equals("")) {
            log.error("获取js-SDK成功：" + tmp1 + " getTime: " + new Date().toLocaleString());
            wxObj.jsSdk = tmp1;
        } else {
            log.error("获取js-SDK失败：" + " getTime: " + new Date().toLocaleString());
        }

    }
}
